package cn.itcast.tool;


import cn.hutool.core.util.StrUtil;
import cn.itcast.domain.Package;
import cn.itcast.service.IPackageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class MyTool {
    @Autowired
    private IPackageService PackageService;

    @Tool(description = "根据多种条件筛选套餐：支持按月租价格范围、流量需求、通话时长和附加服务类型进行精确匹配")
    public List<Package> queryComboPackage(@ToolParam(required = false, description = "套餐查询条件") PackageQuery query) {
        if (query == null) {
            //如果查询条件全为空
            return PackageService.list();//直接查全部
        }
        
        QueryWrapper<Package> wrapper = buildQueryWrapper(query);
        return PackageService.list(wrapper);
    }
    
    /**
     * 构建查询条件
     * @param query 查询参数
     * @return QueryWrapper
     */
    private QueryWrapper<Package> buildQueryWrapper(PackageQuery query) {
        QueryWrapper<Package> wrapper = new QueryWrapper<>();
        // 月租区间
        addMonthlyRentCondition(wrapper, query);
        // 流量模糊匹配
        addFlowCondition(wrapper, query);
        // 通话时长区间
        addCallDurationCondition(wrapper, query);
        // 附加服务模糊匹配
        addAdditionalServiceCondition(wrapper, query);
        return wrapper;
    }
    private void addMonthlyRentCondition(QueryWrapper<Package> wrapper, PackageQuery query) {
        if (query.getMinMonthlyRent() != null) {
            wrapper.ge("monthly_rent", query.getMinMonthlyRent());
        }
        if (query.getMaxMonthlyRent() != null) {
            wrapper.le("monthly_rent", query.getMaxMonthlyRent());
        }
    }
    private void addFlowCondition(QueryWrapper<Package> wrapper, PackageQuery query) {
        if (StrUtil.isNotBlank(query.getFlow())) {
            wrapper.like("flow", query.getFlow());
        }
    }
    private void addCallDurationCondition(QueryWrapper<Package> wrapper, PackageQuery query) {
        if (query.getMinCallDuration() != null) {
            wrapper.ge("call_duration", query.getMinCallDuration());
        }
    }
    private void addAdditionalServiceCondition(QueryWrapper<Package> wrapper, PackageQuery query) {
        if (StrUtil.isNotBlank(query.getAdditionalService())) {
            wrapper.like("additional_service", query.getAdditionalService());
        }
    }
}